﻿//---本地头文件
#include "bslWidgetPoint.h"
#include "ui_bslWidgetPoint.h"
#include <QScrollArea>





bslWidgetPoint::bslWidgetPoint(QWidget *parent)
    : QWidget(parent)
    , ui(new Ui::bslWidgetPoint)
{
    ui->setupUi(this);


     m_pQVBoxLayoutHole = new QVBoxLayout(this);
    m_pQVBoxLayout = new QVBoxLayout();

     scrollWidget = new QWidget(this);


    {
        QPushButton* pQPushButton = new QPushButton(this);
        pQPushButton->setText("类型");
        pQPushButton->setFixedHeight(60);



        bslSixPushButton*    pbslSixPushButton        = new bslSixPushButton(pQPushButton);
        pbslSixPushButton->pPushButton0->setIcon(QIcon(":/Icon.ico"));
        pbslSixPushButton->pPushButton1->setIcon(QIcon(":/Icon.ico"));
        pbslSixPushButton->pPushButton2->setIcon(QIcon(":/Icon.ico"));
        pbslSixPushButton->pPushButton3->setIcon(QIcon(":/Icon.ico"));
        pbslSixPushButton->pPushButton4->setIcon(QIcon(":/Icon.ico"));
        pbslSixPushButton->pPushButton5->setIcon(QIcon(":/Icon.ico"));


        //---添加布局
        QGridLayout*    pQGridLayout = new QGridLayout(this);
        pQGridLayout->addWidget(pQPushButton,0,0,1,2);
        pQGridLayout->addWidget(pbslSixPushButton,1,1);
        m_bIfRemoveWidget1 = false;
        m_pQVBoxLayout->addLayout(pQGridLayout);



        //---布局缩放
        connect(pQPushButton, &QPushButton::clicked, this,
                [=]()
        {

            if(!m_bIfRemoveWidget1)
            {
                pQGridLayout->removeWidget(pbslSixPushButton);
                pbslSixPushButton->setParent(pQPushButton);

                RefreshDialogSize();
                m_bIfRemoveWidget1 = true;
            }
            else
            {
                pQGridLayout->addWidget(pbslSixPushButton,1,1);
                RefreshDialogSize();
                m_bIfRemoveWidget1 = false;
            }
        });
    }


    {
        QPushButton* pQPushButton = new QPushButton(this);
        pQPushButton->setIcon(QIcon(""));
        pQPushButton->setText("点位置");
        pQPushButton->setFixedHeight(60);



        bslLabelAndLabel*    pLabelAndLabel = new bslLabelAndLabel(pQPushButton);

        pLabelAndLabel->pLabel0->setText("指定点");



        //---添加布局
        QGridLayout*    pQGridLayout = new QGridLayout(this);
        pQGridLayout->addWidget(pQPushButton,0,0,1,2);
        pQGridLayout->addWidget(pLabelAndLabel,1,1);
        m_bIfRemoveWidget2 = false;//更新布局信息
        m_pQVBoxLayout->addLayout(pQGridLayout);




        //---布局缩放
        connect(pQPushButton, &QPushButton::clicked, this,
                [=]()
        {

           if(!m_bIfRemoveWidget2)
           {
               pQGridLayout->removeWidget(pLabelAndLabel);
               pLabelAndLabel->setParent(pQPushButton);
               RefreshDialogSize();
               m_bIfRemoveWidget2 = true;
           }
           else
           {
               pQGridLayout->addWidget(pLabelAndLabel,1,1);
               RefreshDialogSize();

               m_bIfRemoveWidget2 = false;
           }
        });

    }


    {
        bslLabelAndPushButton* pPushButton = new bslLabelAndPushButton(this);
        pPushButton->m_pLabel->setText("输出坐标");
        m_pQVBoxLayout->addWidget(pPushButton);


        bslLabelAndComboBox*    pbslLabelAndComboBox        = new bslLabelAndComboBox();
        pbslLabelAndComboBox->pLabel->setText("参考");
        pbslLabelAndComboBox->pComboBox->addItem(tr("工作坐标系"));
        pbslLabelAndComboBox->pComboBox->addItem(tr("绝对"));
        pPushButton->mAddSubWidget(pbslLabelAndComboBox);




        bslLabelAndDoubelEdit*  pbslbslLabelAndDoubelEditX  = new bslLabelAndDoubelEdit();
        pbslbslLabelAndDoubelEditX->pLabel->setText("x");
        pPushButton->mAddSubWidget(pbslbslLabelAndDoubelEditX);



        bslLabelAndDoubelEdit*  pbslbslLabelAndDoubelEditY   = new bslLabelAndDoubelEdit();
        pbslbslLabelAndDoubelEditY->pLabel->setText("y");
        pPushButton->mAddSubWidget(pbslbslLabelAndDoubelEditY);



        bslLabelAndDoubelEdit*  pbslbslLabelAndDoubelEditZ   = new bslLabelAndDoubelEdit();
        pbslbslLabelAndDoubelEditZ->pLabel->setText("z");
        pPushButton->mAddSubWidget(pbslbslLabelAndDoubelEditZ);
    }



    //---属性浏览组件
    {
        QtTreePropertyBrowser * pTreeBrowser = new QtTreePropertyBrowser(this);
        pTreeBrowser->setMinimumHeight(400);
        m_pQVBoxLayout->addWidget(pTreeBrowser);

        QtVariantPropertyManager *pManager = new QtVariantPropertyManager(pTreeBrowser);
        QtVariantEditorFactory *pFactory = new QtVariantEditorFactory(pTreeBrowser);
        pTreeBrowser->setFactoryForManager(pManager,pFactory);


        //---创建emun类型的item
        QtVariantProperty *pVariantProperty = pManager->addProperty(QtVariantPropertyManager::enumTypeId(),"enum");
        QString range = "A,B,C,D,E";
        QStringList enumnameslist = range.split(",");
        pVariantProperty->setAttribute("enumNames",enumnameslist);
        int selectNo = -1;
        QString value = "C";
        if(enumnameslist.contains(value)) {
            selectNo = enumnameslist.indexOf(value);
        }
        pVariantProperty->setValue(selectNo);
        pTreeBrowser->addProperty(pVariantProperty);



        //创建flag类型的item
        pVariantProperty = pManager->addProperty(QtVariantPropertyManager::flagTypeId(),"flag");
        //设置列表

        range = "AA|AB|AC|AD|AE";
        QStringList flaglist = range.split("|");
        pVariantProperty->setAttribute("flagNames",flaglist );
        selectNo = 0;
        QString valuestr = "AB|AD";
        QStringList valuelist = valuestr.split("|");
        foreach(auto va,valuelist) {
            if(flaglist.contains(va)) {
                selectNo |= 0x01 << flaglist.indexOf(va);
            }
        }
        pVariantProperty->setValue(selectNo);
        pTreeBrowser->addProperty(pVariantProperty);
    }



//    //---添加竖直缩放自由度
//    {
//        m_pQVBoxLayout->addStretch();
//    }



//    //---添加对话框按钮
//    {

//        QDialogButtonBox* pQDialogButtonBox = new QDialogButtonBox(this);
//        pQDialogButtonBox->addButton( "OK", QDialogButtonBox::YesRole);
//        pQDialogButtonBox->addButton( "NO", QDialogButtonBox::NoRole);
//        connect(pQDialogButtonBox, SIGNAL(accepted()), this, SLOT(close()));
//        connect(pQDialogButtonBox, SIGNAL(rejected()), this, SLOT(close()));


//        m_pQVBoxLayout->addWidget(pQDialogButtonBox);
//    }



    //创建滚动区域

    {
        m_pQVBoxLayout->setMargin(0);
        m_pQVBoxLayout->setSpacing(0);

        m_pQVBoxLayout->addStretch();
        scrollWidget->setLayout(m_pQVBoxLayout);


        QScrollArea* scrollArea = new QScrollArea(this);
        scrollArea->setWidget(scrollWidget);       //给ScrollArea添加内部部件
        scrollArea->setWidgetResizable(true);			//自动调整大小





        m_pQVBoxLayoutHole->addWidget(scrollArea);
    }



    //---添加竖直缩放自由度
    {
//        m_pQVBoxLayoutHole->addStretch();
    }



    //---添加对话框按钮
    {

         pQDialogButtonBox = new QDialogButtonBox(this);
         pQDialogButtonBox->addButton( "OK", QDialogButtonBox::YesRole);
         pQDialogButtonBox->addButton( "NO", QDialogButtonBox::NoRole);
         connect(pQDialogButtonBox, SIGNAL(accepted()), this, SLOT(close()));
         connect(pQDialogButtonBox, SIGNAL(rejected()), this, SLOT(close()));


         m_pQVBoxLayoutHole->addWidget(pQDialogButtonBox);
    }













    setLayout(m_pQVBoxLayoutHole);
     m_pQVBoxLayoutHole->setSizeConstraint(QLayout::SetMinimumSize);
}



bslWidgetPoint::~bslWidgetPoint()
{
    delete ui;
}



void bslWidgetPoint::RefreshDialogSize()
{
    m_pAnimation = new QPropertyAnimation(this, ""); //qt动画类
    m_pAnimation->setDuration(600);       //动画持续时间500ms
    m_pAnimation->setStartValue(300);     //动画开始值
    m_pAnimation->setEndValue(600);       //动画结束值

//    m_pQVBoxLayout->setSizeConstraint(QLayout::SetNoConstraint);
//    this->setMinimumSize(0,0);

    connect(m_pAnimation, &QPropertyAnimation::valueChanged,
            [=]()
    {
        this->resize(this->width(),scrollWidget->height()+ pQDialogButtonBox->height());

    });
    m_pAnimation->start(QAbstractAnimation::DeleteWhenStopped);
//    m_pQVBoxLayout->setSizeConstraint(QLayout::SetDefaultConstraint);
}
